//
//  homeViewController.m
//  YuErmuYing
//
//  Created by Tiancheng on 16/8/8.
//  Copyright © 2016年 Tiancheng. All rights reserved.
//

#import "homeViewController.h"
#import "Macros.h"
#import "UIColor+RandomColor.h"
#import "HttpRequest.h"
#import "youWenBiDaViewController.h"
#import "cuiRuShiViewController.h"
#import "xinShengErHuLiViewController.h"
#import "yueShaoViewController.h"
#import "baoMaShangChengViewController.h"
#import "yiYuanDuoBaoViewController.h"
#import "shaiDanViewController.h"
#import "jiFenDuiHuanViewController.h"


@interface homeViewController ()<UIScrollViewDelegate>
@property (weak, nonatomic) IBOutlet UIView *backGroundView;
//图片的轮播图
@property (weak, nonatomic) IBOutlet UIScrollView *imageScrollView;
@property(nonatomic,retain)UIPageControl*upc;

//八个按钮小的方块长度
@property(nonatomic,assign)float smallWidth;
//八个按钮
@property(nonatomic,retain)UIButton *picButton;
//通过数组来获得第二行按钮的图片的位置
@property(nonatomic,copy)NSMutableArray *pic_Array;
//长条背景图
@property(nonatomic,copy)UIView *uview0;
//喇叭里的滚动字幕
@property(nonatomic,retain)UILabel *laBarLabel1;
//喇叭的view
@property(nonatomic,retain)UIView*next_view;
@end

@implementation homeViewController
@synthesize pic_Array,uview0,imageScrollView,upc,next_view;
-(void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBarHidden=YES;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    self.smallWidth=screenWidth/4;
//   创建8个按钮
    [self createEightButton];

}
//创建8个按钮
-(void)createEightButton
{
    //创建轮播图
    [self  createHeaderScrollCiew];
    pic_Array=[NSMutableArray array];
    //八个按钮的图片名字
    NSArray *picArray=[NSArray arrayWithObjects:@"youWenBiDa",@"cuiRuShi",@"xinShengErHuLi",@"yueShao", @"baoMaShangCheng",@"yiYuanDuoBao",@"shaiDan",@"exchange",nil];
    //八个按钮的文字
    NSArray *picNameArray=[NSArray arrayWithObjects:@"有问必答",@"催乳师",@"新生护理",@"月嫂",@"宝妈积分",@"一元夺宝",@"晒单",@"积分兑换", nil];
    for (int i=0; i<2; i++)
    {
        for (int j=0; j<4; j++)
        {
            //20=0.0534*screenWidth
            self.picButton=[[UIButton alloc]initWithFrame:CGRectMake(self.smallWidth*j, (self.smallWidth-0.0534*screenWidth)*i+140, self.smallWidth, self.smallWidth-0.0534*screenWidth)];
            self.picButton.tag=j+i*4;
            self.picButton.backgroundColor=[UIColor whiteColor];
            [self.picButton addTarget:self action:@selector(onClick1:) forControlEvents:UIControlEventTouchUpInside];
            [self.backGroundView addSubview:self.picButton];
            //25=0.0667*screenWidth
            //5=0.0133*screenWidth
            //40=0.1067*screenWidth
            UIImageView *pic_ImageView=[[UIImageView alloc]initWithFrame:CGRectMake(0.0667*screenWidth, 0.0133*screenWidth, 0.1067*screenWidth, 0.1067*screenWidth)];
            pic_ImageView.image=[UIImage imageNamed:picArray[j+i*4]];
            [self.picButton addSubview:pic_ImageView];
            //15＝0.04*screenWidth
            //50＝0.1333*screenWidth
            //60＝0.16*screenWidth
            //20＝0.0533*screenWidth
            UILabel *picLabel=[[UILabel alloc]initWithFrame:CGRectMake(0.04*screenWidth, 0.1333*screenWidth,0.16*screenWidth,0.0533*screenWidth)];
//            picLabel.backgroundColor=[UIColor whiteColor];
            picLabel.text=picNameArray[j+i*4];
            picLabel.textColor=[UIColor colorWithRed:216/255.0 green:27/255.0 blue:96/255.0 alpha:1];
            picLabel.font=[UIFont systemFontOfSize:15];
            //字体居中
            picLabel.textAlignment=NSTextAlignmentCenter;
            [self.picButton addSubview:picLabel];
            [pic_Array addObject:self.picButton];
        }
    }
    if (pic_Array[4])
    {
        [self createActiveButton];
    }
}
//创建轮播图
-(void)createHeaderScrollCiew
{
    CGSize size=CGSizeMake(screenWidth*3, 0);
    imageScrollView.contentSize=size;
    for (int i=0; i<3; i++)
    {
        UIImageView *imgView=[[UIImageView alloc]initWithFrame:CGRectMake(screenWidth*i, 0, screenWidth, 140)];
//        NSString* str=[NSString stringWithFormat:@"car_1%d.png",i];
                NSString* str=[NSString stringWithFormat:@"banner"];
        UIImage *img=[UIImage imageNamed:str];
        imgView.image=img;
        [imageScrollView addSubview:imgView];
    }
    imageScrollView.pagingEnabled=YES;
    imageScrollView.delegate=self;
    //scroll.backgroundColor=[UIColor blueColor];
    imageScrollView.showsHorizontalScrollIndicator=NO;
    imageScrollView.showsVerticalScrollIndicator=NO;
    upc=[[UIPageControl alloc]initWithFrame:CGRectMake((screenWidth-150)/2+60, 90, 150, 66)];
    upc.numberOfPages=3;
    upc.currentPage=0;
    [self.backGroundView addSubview:upc];
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    upc.currentPage=scrollView.contentOffset.x/screenWidth;
}
//创建活动专区的界面
-(void)createActiveButton
{
   UIButton *btn=pic_Array[4];
   float btn_y=btn.frame.origin.y+self.smallWidth-0.0534*screenWidth;
    //119=0.3174*screenWidth
    uview0=[[UIView alloc]initWithFrame:CGRectMake(0, btn_y, screenWidth, 0.3174*screenWidth)];
    uview0.backgroundColor=[UIColor whiteColor];
    [self.backGroundView addSubview:uview0];
    //灰色背景框
    //18=0.048*screenWidth
    UIView *uview1=[[UIView alloc]initWithFrame:CGRectMake(0, 0, screenWidth, 0.048*screenWidth)];
    uview1.backgroundColor=[UIColor colorWithRed:243/255.0 green:243/255.0 blue:243/255.0 alpha:1];
    [uview0 addSubview:uview1];
    //2=0.00533*screenWidth
    //100=0.2667*screenWidth
    //11=0.02934*screenWidth
    UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(0, 0.00533*screenWidth,0.2667*screenWidth, 0.02934*screenWidth)];
    label.text=@"活动专区";
    label.textColor=[UIColor blackColor];
    label.font=[UIFont systemFontOfSize:15];
    [uview1 addSubview:label];
    NSArray *picArray1=[NSArray arrayWithObjects:@"xinRenDuoBao",@"baoBaoTiYanZhao",@"yingErYouYongTiYan",@"chuanZhaoPianYingJiangPin", nil];
    for (int i=0; i<2; i++)
    {
        for (int j=0; j<2; j++)
        {
            //18=0.048*screenWidth
            //50.5=0.13467*screenWidth
            UIImageView *picImageView=[[UIImageView alloc]initWithFrame:CGRectMake(0+j*screenWidth/2, 0.048*screenWidth+0.13467*screenWidth*i, screenWidth/2, 0.13467*screenWidth)];
            picImageView.tag=j+i*2;
            picImageView.image=[UIImage imageNamed:picArray1[j+i*2]];
            [uview0 addSubview:picImageView];
            //添加手势
            UITapGestureRecognizer *tap=[[UITapGestureRecognizer  alloc ] initWithTarget:self action : @selector (onClick2:)];
            picImageView.userInteractionEnabled=YES;
            [picImageView addGestureRecognizer:tap];
        }
    }
    if (uview0)
    {
        [self createScrollLabel];
    }
    
}
//创建滚动字幕
-(void)createScrollLabel
{
    float uview_y=uview0.frame.origin.y+0.3174*screenWidth;
    next_view=[[UIView alloc]initWithFrame:CGRectMake(0, uview_y, screenWidth, 23)];
    next_view.backgroundColor=[UIColor whiteColor];
    [self.backGroundView addSubview:next_view];
    UIImageView *imageView=[[UIImageView alloc]initWithFrame:CGRectMake(20, 2,18, 18)];
    imageView.image=[UIImage imageNamed:@"laBa"];
    [next_view addSubview:imageView];
    
    
    
    self.laBarLabel1=[[UILabel alloc]initWithFrame:CGRectMake(71, 2, 100, 15)];
    NSString *s=@"初六的妈妈";
    self.laBarLabel1.text=@"初六的妈妈";
    self.laBarLabel1.textColor=[UIColor blackColor];
    self.laBarLabel1.font=[UIFont systemFontOfSize:14];
    self.laBarLabel1.backgroundColor=[UIColor orangeColor];
    //*******************************************
    //可以换行
    [self.laBarLabel1 setNumberOfLines:0];
    CGSize size = CGSizeMake(320,2000);
    CGSize labelsize = [s sizeWithFont:self.laBarLabel1.font constrainedToSize:size lineBreakMode:NSLineBreakByWordWrapping];
    [self.laBarLabel1 setFrame:CGRectMake(71,2, labelsize.width, labelsize.height)];
    [next_view addSubview:self.laBarLabel1];
    //创建一元夺宝的活动界面
    [self createYiYuanDuoBao];
}
//创建一元夺宝的活动界面
-(void)createYiYuanDuoBao
{
    float uview_y1=next_view.frame.origin.y+23;
    UIView *next_uview1=[[UIView alloc]initWithFrame:CGRectMake(0, uview_y1, screenWidth, 24)];
    next_uview1.backgroundColor=[UIColor colorWithRed:243/255.0 green:243/255.0 blue:243/255.0 alpha:1];
    [self.backGroundView addSubview:next_uview1];
    UILabel *label1=[[UILabel alloc]initWithFrame:CGRectMake(20, 2, 200, 20)];
    label1.text=@"一元抢宝一马上揭晓";
    label1.textColor=[UIColor colorWithRed:216/255.0 green:29/255.0 blue:96/255.0 alpha:1.0];
    [next_uview1 addSubview:label1];
    UIButton *btn=[[UIButton alloc]initWithFrame:CGRectMake(screenWidth-80, 2, 80, 20)];
    [btn setTitle:@"更多" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor colorWithRed:216/255.0 green:29/255.0 blue:96/255.0 alpha:1.0] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(onClick3:) forControlEvents:UIControlEventTouchUpInside];
    [next_uview1 addSubview:btn];
    //获取一元夺宝的时间,以及公布的界面
    [self updateYiYuanDuoBao];
}
//八个按钮的点击方法
-(void)onClick1:(UIButton*)sender
{
    NSInteger integer=sender.tag;
    int num=(int)integer;
    if (num==0)
    {
        youWenBiDaViewController *ywbc=[youWenBiDaViewController new];
        [self.navigationController pushViewController:ywbc animated:YES];
    }else if (num==1)
    {
        cuiRuShiViewController *crsvc=[cuiRuShiViewController new];
        [self.navigationController pushViewController:crsvc animated:YES];
    }else if (num==2)
    {
        xinShengErHuLiViewController *xsehlvc=[xinShengErHuLiViewController new];
        [self.navigationController pushViewController:xsehlvc animated:YES];
    }else if (num==3)
    {
        yueShaoViewController *ysvc=[yueShaoViewController new];
        [self.navigationController pushViewController:ysvc animated:YES];
        
    }else if (num==4)
    {
//        baoMaShangChengViewController *bmsc=[baoMaShangChengViewController new];
//        [self.navigationController pushViewController:bmsc animated:YES];
//        TCLYBabyHomeViewController * babyHomeViewCtr = [[TCLYBabyHomeViewController alloc] init];
//        [self.navigationController pushViewController:babyHomeViewCtr animated:YES];
    }else if (num==5)
    {
        yiYuanDuoBaoViewController *ydbv=[yiYuanDuoBaoViewController new];
        [self.navigationController pushViewController:ydbv animated:YES];
    }else if (num==6)
    {
        shaiDanViewController *sdvc=[shaiDanViewController new];
        [self.navigationController pushViewController:sdvc animated:YES];
    }else if (num==7)
    {
//        jiFenDuiHuanViewController *jfdh =[jiFenDuiHuanViewController new];
//        [self.navigationController pushViewController:jfdh animated:YES];
//        TCLYIntergralViewController * intergralViewCtr = [[TCLYIntergralViewController alloc] init];
//        [self.navigationController pushViewController:intergralViewCtr animated:YES];
    }
}
//四个长条图片的点击方法
-(void)onClick2:(UITapGestureRecognizer *)sender
{
    NSInteger integer=sender.view.tag;
    int num=(int)integer;
    if (num==0)
    {
        NSLog(@"进入图片1");
    }else if (num ==1)
    {
        NSLog(@"进入图片2");

    }else if (num ==2)
    {
        NSLog(@"进入图片3");
        
    }else if (num ==3)
    {
        NSLog(@"进入图片4");
        
    }
}
//一元夺宝里的更多的按钮方法
-(void)onClick3:(UIButton*)sender
{
    NSLog(@"一元夺宝,详情方法");

}
//一元夺宝更新四个
-(void)updateYiYuanDuoBao
{
    //网络请求
    NSString *urlString=[NSString stringWithFormat:@"http://112.74.114.1/parenting/index.php/Admin/Indiana/indiana"];
    HttpRequest *httpRequest=[HttpRequest sharedInstance];
    [httpRequest postWithURLString:urlString parameters:nil success:^(id responseObject)
     {
         id json1 = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
         //
         NSArray *json_Array1=[json1 objectForKey:@"data"];
//         NSLog(@"json_Array1.count=%ld",json_Array1.count);
//         NSLog(@"json1=%@",json1);
     } failure:^(NSError *error) {
//         NSLog(@"error=%@",error);
     }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
